package cn.edu.sgu.www.mhxysy.controller.prop;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.prop.PropCategoryDTO;
import cn.edu.sgu.www.mhxysy.easyui.component.impl.Tree;
import cn.edu.sgu.www.mhxysy.entity.prop.PropCategory;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.prop.PropCategoryService;
import cn.edu.sgu.www.mhxysy.vo.treegrid.PropCategoryTreeGrid;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "道具类型管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/prop_category", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class PropCategoryController {

    private final PropCategoryService propCategoryService;

    @Autowired
    public PropCategoryController(PropCategoryService propCategoryService) {
        this.propCategoryService = propCategoryService;
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult<String> upload(MultipartFile file) throws IOException {
        String url = propCategoryService.upload(file);

        return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
    }

    @ApiOperation("添加道具类型")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated PropCategoryDTO propCategoryDTO) {
        propCategoryService.insert(propCategoryDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除道具类型")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "道具类型ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        propCategoryService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改道具类型")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated PropCategoryDTO propCategoryDTO) {
        propCategoryService.updateById(propCategoryDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID修改道具图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "道具类型ID", required = true),
            @ApiImplicitParam(name = "image", value = "图片地址", required = true)
    })
    @RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
    public JsonResult<Void> updateImageById(
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
            @NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
        propCategoryService.updateImageById(id, image);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID查询道具类型")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<PropCategory> selectById(
            @ApiParam(name = "id", value = "道具类型ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        PropCategory propCategory = propCategoryService.selectById(id);

        return JsonResult.success(null, propCategory);
    }

    @ApiOperation("查询道具类型列表")
    @RequestMapping(value = "/selectByType", method = RequestMethod.GET)
    public JsonResult<List<PropCategory>> selectByType(
            @ApiParam(name = "type", value = "道具类型", required = true)
            @NotNull(message = "道具类型" + CustomMessages.ID_NOT_NULL) Integer type) {
        List<PropCategory> list = propCategoryService.selectByType(type);

        return JsonResult.success(null, list);
    }

    @ApiOperation("查询道具类型的主题表")
    @RequestMapping(value = "/selectThemes", method = RequestMethod.GET)
    public JsonResult<List<PropCategory>> selectThemes(
            @ApiParam(name = "propId", value = "道具类型ID", required = false)
            @RequestParam(required = false) Integer propId) {
        List<PropCategory> list = propCategoryService.selectThemes(propId);

        return JsonResult.success(null, list);
    }

    @ApiOperation("获取道具类型树")
    @RequestMapping(value = "/listTree", method = RequestMethod.GET)
    public JsonResult<List<Tree<PropCategory>>> listTree() {
        List<Tree<PropCategory>> list = propCategoryService.listTree();

        return JsonResult.success(null, list);
    }

    @ApiOperation("获取道具类型树形表格")
    @RequestMapping(value = "/listTreeGrid", method = RequestMethod.GET)
    public JsonResult<List<PropCategoryTreeGrid>> listTreeGrid() {
        List<PropCategoryTreeGrid> list = propCategoryService.listTreeGrid();

        return JsonResult.success(null, list);
    }

    @ApiOperation("获取道具类型树形组合表格")
    @RequestMapping(value = "/listComboTreeGrid", method = RequestMethod.GET)
    public JsonResult<List<PropCategoryTreeGrid>> listComboTreeGrid() {
        List<PropCategoryTreeGrid> list = propCategoryService.listComboTreeGrid();

        return JsonResult.success(null, list);
    }

}